package com.heima.wemedia.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 在spring项目启动完成之后会自动执行里面的 run
 * 从而初始化DFA结构
 */
@Component
public class WmNewsApplicationRunner implements ApplicationRunner {
    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //初始化DFA数据结构
        //1.查询数据库中的敏感词信息
        List<WmSensitive> wmSensitiveList = wmSensitiveMapper.selectList(null);
        List<String> strings = wmSensitiveList.stream().map(wmSensitive -> {
            String sensitives = wmSensitive.getSensitives();
            return sensitives;
        }).collect(Collectors.toList());

        //2.初始DFA数据库
        SensitiveWordUtil.initMap(strings);
    }
}
